/**
 *
 */
package gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import server.ServerConfiguration;

/**
 * @author Julius Ollesch
 * 
 */
public class LoginDialog implements ServerConfiguration {

	public static void main(String[] args) {
		new LoginDialog();

	}

	private Shell sShell = null; // @jve:decl-index=0:visual-constraint="0,0"
	private Text textBenutzername = null;
	private Label labelBenutzername = null;
	private Label labelPasswort = null;
	private Text textPasswort = null;
	private Button buttonOk = null;
	private Button buttonCancel = null;

	private String anmeldeInfo[] = new String[2];

	public boolean open = false;

	/**
	 * This method initializes sShell
	 */

	public LoginDialog() {
		super();

	}

	private void close() {
		sShell.dispose();
	}

	private Display erzeugeGUI() {
		GridData gridData3 = new GridData();
		gridData3.grabExcessHorizontalSpace = true;
		gridData3.horizontalAlignment = GridData.FILL;
		gridData3.verticalAlignment = GridData.CENTER;
		gridData3.grabExcessVerticalSpace = true;
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.FILL;
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.grabExcessVerticalSpace = true;
		gridData2.verticalAlignment = GridData.CENTER;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.horizontalSpan = 2;
		gridData1.verticalAlignment = GridData.CENTER;
		GridData gridData = new GridData();
		gridData.horizontalSpan = 2;
		gridData.verticalAlignment = GridData.CENTER;
		gridData.horizontalAlignment = GridData.FILL;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.makeColumnsEqualWidth = true;
		Display display = Display.getCurrent();

		if (display == null) {
			display = new Display();
		}
		sShell = new Shell(display, SWT.DIALOG_TRIM | SWT.SYSTEM_MODAL);
		sShell.setText("Datenbank Administrator erstellen");
		sShell.setLayout(gridLayout);
		sShell.setSize(new Point(373, 129));
		labelBenutzername = new Label(sShell, SWT.NONE);
		labelBenutzername.setText("Benutzername:");
		textBenutzername = new Text(sShell, SWT.BORDER);
		textBenutzername.setLayoutData(gridData);
		labelPasswort = new Label(sShell, SWT.NONE);
		labelPasswort.setText("Passwort:");
		textPasswort = new Text(sShell, SWT.BORDER | SWT.PASSWORD);
		textPasswort.setLayoutData(gridData1);
		Label filler = new Label(sShell, SWT.NONE);

		Label filler2 = new Label(sShell, SWT.NONE);
		Label filler1 = new Label(sShell, SWT.NONE);
		buttonOk = new Button(sShell, SWT.NONE);
		buttonOk.setText("Anmelden");
		buttonOk.setLayoutData(gridData2);
		buttonOk
				.addTraverseListener(new org.eclipse.swt.events.TraverseListener() {
					public void keyTraversed(
							org.eclipse.swt.events.TraverseEvent e) {
						ok();

					}
				});
		buttonOk.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			@Override
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
				ok();

			}
		});
		buttonCancel = new Button(sShell, SWT.NONE);
		buttonCancel.setText("Abbrechen");
		buttonCancel.setLayoutData(gridData3);
		buttonCancel
				.addTraverseListener(new org.eclipse.swt.events.TraverseListener() {
					public void keyTraversed(
							org.eclipse.swt.events.TraverseEvent e) {
						anmeldeInfo[0] = "";
						anmeldeInfo[1] = "";
						close();
					}
				});
		buttonCancel
				.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
					@Override
					public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
						anmeldeInfo[0] = "";
						anmeldeInfo[1] = "";
						close();
					}
				});
		return display;

	}

	private void ok() {
		anmeldeInfo[0] = textBenutzername.getText();
		anmeldeInfo[1] = textPasswort.getText();
		close();
	}

	public String[] open() {
		Display display = erzeugeGUI();
		sShell.open();
		this.open = true;
		while (!sShell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		// display.dispose();
		this.open = false;
		return anmeldeInfo;
	}

}
